﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MySQLite.Models
{
    public class Page
    {

        private bool pageFlag = true;
        /// <summary>
        /// true分页，false不分页
        /// </summary>
        public bool PageFlag
        {
            get
            {
                return pageFlag;
            }
            set
            {
                pageFlag = value;
            }
        }

        private int pageNo = 1;
        /// <summary>
        /// 起始页
        /// </summary>
        public int PageNo
        {
            get
            {
                return pageNo;
            }
            set
            {
                pageNo = value;
            }
        }

        private int pageRow = 10;
        /// <summary>
        /// 每页条数
        /// </summary>
        public int PageRow
        {
            get
            {
                return pageRow;
            }
            set
            {
                pageRow = value;
            }
        }

        private int totalCount;
        /// <summary>
        /// 总条数
        /// </summary>
        public int TotalCount
        {
            get
            {
                return totalCount;
            }
            set
            {
                totalCount = value;
                if (value > 0)
                {
                    totalPage = (value / pageRow) + ((value % pageRow) > 0 ? 1 : 0);
                }
            }
        }

        private int totalPage;
        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPages
        {
            get
            {
                return totalPage;
            }
            set
            {
                totalPage = value;
            }
        }

        private bool hasPre;
        /// <summary>
        /// 
        /// </summary>
        public bool HasPre
        {
            get
            {
                return hasPre;
            }
            set
            {
                hasPre = value;
            }
        }

        private bool hasNext;
        /// <summary>
        /// 
        /// </summary>
        public bool HasNext
        {
            get
            {
                return hasNext;
            }
            set
            {
                hasNext = value;
            }
        }

        private int prePage;
        /// <summary>
        /// 
        /// </summary>
        public int PrePage
        {
            get
            {
                return prePage;
            }
            set
            {
                prePage = value;
            }
        }

        private int nextPage;
        /// <summary>
        /// 
        /// </summary>
        public int NextPage
        {
            get
            {
                return nextPage;
            }

            set
            {
                nextPage = value;
            }
        }

        /// <summary>
        /// 设置是否有上一页或者下一页
        /// </summary>
        public void SetPageState()
        {
            if (pageFlag)
            {
                TotalPages = TotalCount / PageRow + (TotalCount % PageRow == 0 ? 0 : 1);
                if (PageNo > 1)
                    HasPre = true;
                if (PageNo != TotalPages)
                    HasNext = true;
            }
            else
            {
                TotalPages = 1;
                HasPre = false;
                HasNext = false;
            }
        }
    }

    public class Page<T> : Page
    {
        private T? _pageModel;
        public T? PageModel
        {
            get { return _pageModel; }
            set { _pageModel = value; }
        }
    }

    public class Pages<T> : Page
    {
        private List<T>? _pageModels;
        public List<T>? PageModels
        {
            get { return _pageModels; }
            set { _pageModels = value; }
        }
    }
}
